﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcmyjobs.Web.Admin
{
    public partial class newslist : System.Web.UI.Page
    {
        lcmyjobs.BLL.news bub = new lcmyjobs.BLL.news();
        DataSet ds;
        int count = 0;
        int id;

        protected void Page_Load(object sender, EventArgs e)
        {
            id = int.Parse(Application["webid"].ToString());
            if (!IsPostBack)
            {
                if (Session["AdminLogin2"] != null)
                {
                    getcout(id);
                    btnDelete.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");

                    //bindData(); //使用url分页，只需在分页事件处理程序中绑定数据即可，无需在Page_Load中绑定，否则会导致数据被绑定两次
                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript(this, "alert('请先登陆！');top.location.href='adminlogin2.aspx'");
                }

            }

        }
        protected void getcout(int atid)
        {
            ds = bub.PageNews(atid, 1, AspNetPager1.PageSize, out count);
            AspNetPager1.RecordCount = count;
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
        protected void bind(int atid)
        {
            ds = bub.PageNews(atid, AspNetPager1.StartRecordIndex, AspNetPager1.PageSize, out count);
            AspNetPager1.RecordCount = count;
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }



        //protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        //{
        //    id = int.Parse(Application["webid"].ToString());
        //    bind(id);

        //}

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
            //    linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
            bub.deletenews(ID);
            bind(id);
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)GridView1.Rows[i].FindControl("chku");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GridView1.DataKeys[i].Value != null)
                    {
                        idlist += GridView1.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }




        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bub.deletenews(idlist);
            bind(id);
        }

      
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            id = int.Parse(Application["webid"].ToString());
            bind(id);
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_rm")
            {
                if (e.CommandArgument != null)
                {
                    int nid = Convert.ToInt32(e.CommandArgument);

                    lcmyjobs.BLL.news news = new lcmyjobs.BLL.news();


                    ds = news.GetNews(nid);
                    string isRM = "0";
                    string msg = "";
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0]["isRM"].ToString() == "False" || ds.Tables[0].Rows[0]["isRM"].ToString() == "0")
                        {
                            //推荐
                            isRM = "1";
                            msg = "标记热门文章成功!";
      
                        }
                        else
                        {
                            //撤销推荐
                            isRM = "0";
                            msg = "撤销热门成功!";
                        }
                    }

                    if (new lcmyjobs.BLL.news().UpdateWhere("isrm", isRM, nid.ToString()))
                    {
                        ViewState["page"] = this.GridView1.PageIndex;

                        Maticsoft.Common.MessageBox.Show(this, msg);
                        if (Application["webid"] != null)
                        {
                            bind(Convert.ToInt32(Application["webid"]));
                        }
                    }
                }

            }

            if (e.CommandName == "lbn_tj")
            {
                if (e.CommandArgument != null)
                {
                    int nid = Convert.ToInt32(e.CommandArgument);

                    lcmyjobs.BLL.news news = new lcmyjobs.BLL.news();

                    
                    ds = news.GetNews(nid);
                    string isTJ="0";
                    string msg="";
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0]["isTJ"].ToString() == "False" || ds.Tables[0].Rows[0]["isTJ"].ToString() == "0")
                        {
                            //推荐
                            isTJ = "1";
                            msg = "推荐文章成功!";
                          

                        }
                        else
                        {
                            //撤销推荐
                            isTJ = "0";
                            msg = "撤销推荐成功!";
                        }
                    }
                    if (new lcmyjobs.BLL.news().UpdateWhere("istj", isTJ, nid.ToString()))
                    {
                        ViewState["page"] = this.GridView1.PageIndex;

                        Maticsoft.Common.MessageBox.Show(this, msg);
                        if (Application["webid"] != null)
                        {
                            bind(Convert.ToInt32(Application["webid"]));
                        }

                    }
                }

            }

            if (e.CommandName == "lbn_cxtj")
            {

                if (e.CommandArgument != null)
                {
                    int nid = Convert.ToInt32(e.CommandArgument);


                    if (new lcmyjobs.BLL.news().UpdateWhere("istj", "0", nid.ToString()))
                    {
                        ViewState["page"] = this.GridView1.PageIndex;

                        Maticsoft.Common.MessageBox.Show(this, "撤销推荐文章标记成功！");
                        if (Application["webid"] != null)
                        {
                            bind(Convert.ToInt32(Application["webid"]));
                        }
                    }
                }
            }
            if (e.CommandName == "lbn_cxrm")
            {
                if (e.CommandArgument != null)
                {
                    int nid = Convert.ToInt32(e.CommandArgument);


                    if (new lcmyjobs.BLL.news().UpdateWhere("isrm", "0", nid.ToString()))
                    {
                        ViewState["page"] = this.GridView1.PageIndex;

                        Maticsoft.Common.MessageBox.Show(this, "撤销热门文章标记成功！");
                        if (Application["webid"] != null)
                        {
                            bind(Convert.ToInt32(Application["webid"]));
                        }
                    }
                }
            }
        }




    }
}